package com.example.musictutor.services.runnables;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.ImageButton;

import com.example.musictutor.R;
import com.example.musictutor.activities.PlaySongActivity;
import com.example.musictutor.domain.Song;
import com.example.musictutor.domain.SongNote;
import com.example.musictutor.services.listeners.PlaySongCompletionListener;
import com.example.musictutor.services.playfunctionobserver.PlayFunctions;
import com.example.musictutor.services.playfunctionobserver.PlayFunctionsInterface;

public class PlayRunnable implements Runnable, PlayFunctionsInterface {
	private Song song;
	private Context context;
	private PlaySongActivity activity;
	private PlayFunctions playFunctions = new PlayFunctions();
	private boolean isPaused = false;
	private boolean isRestarted = false;
	
	private PlaySongCompletionListener completionListener;
	
	public PlayRunnable(Song song, Context context) {
		super();
		this.song = song;
		this.context = context;
		this.activity = (PlaySongActivity) context;
	}
	
	@Override
	public void run() {	
		int waitTime = 0;
		List<SongNote> notesList = new ArrayList<SongNote>(song.getNotes());
		while(!notesList.isEmpty()) {
			if(isRestarted) {
				notesList = new ArrayList<SongNote>(song.getNotes());
				isRestarted = false;
				activity.runOnUiThread(new Runnable() {
					public void run() {
						activity.functionButtonPressed(PlaySongActivity.PLAY_BUTTON);
					}
				});
			}
			
			if(notesList.size() < 2)
				waitTime = 0;
			else
				waitTime = notesList.get(1).getStartPosition() - notesList.get(0).getStartPosition();
			
			PlayNoteRunnable noteObserver = new PlayNoteRunnable(notesList.get(0), context);
			playFunctions.register(noteObserver);
			noteObserver.setPlaySubject(playFunctions);
			
			Thread thread = new Thread(noteObserver);
			thread.start();
			
			notesList.remove(0);
			
			Log.d("WaitTime", "" + waitTime);
			
			if(waitTime > 0) {
				try {
					Thread.sleep(waitTime);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			while(isPaused) {
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		completionListener.playSongCompleted();
	}

	@Override
	public void playPressed() {
		if(isPaused)
			playFunctions.postMessage(PlayFunctions.PLAY_PRESSED);
		
		isPaused = false;
	}

	@Override
	public void pausePressed() {
		isPaused = true;
		playFunctions.postMessage(PlayFunctions.PAUSE_PRESSED);
	}

	@Override
	public void restartPressed() {
		isPaused = false;
		isRestarted = true;
	}

	public PlaySongCompletionListener getPlaysongCompletionListener() {
		return completionListener;
	}

	public void setPlaysongCompletionListener(
			PlaySongCompletionListener playsongCompletionListener) {
		this.completionListener = playsongCompletionListener;
	}

	
}
